import { ExpeditionOrder } from './../../models/expeditionOrder';
import { Employee } from './../../models/Employee';
import URI from 'urijs';
import { cloundHttpServer, LDCHttpServer, businsessHttpServer } from './http-server'

export function httpResultCallback<T>(result):Promise<T> {
    return new Promise((resolve, reject) => {
        result.then(response => {
            const rData = response.data;
            //老的java接口会这样子返回
            if (typeof rData.code == "undefined") {
                resolve(rData);
            }
            if (rData.code == 200) resolve(rData.data);
            else reject(rData.message);
        }).catch(error => {
            console.log(error);
            reject(error);
        })
    });
}

export interface ParseUrlOption{ id?: string, controller?: string, query?: Object }

export function parseUrl(prefixUrl, options?: ParseUrlOption) {
    // return URI.expand(this.prefixUrl + "/{id}/{controller}", {
    //     id: options && options.id,
    //     controller: options && options.controller
    // }) + URI.buildQuery(options.query);
    let url = prefixUrl;
    if (options && options.id) {
        url += "/" + options.id;
    }
    if (options && options.controller) {
        url += "/" + options.controller;
    }
    if (options && options.query) {
        url += "?";
        const obj = options.query;
        let array = [];
        Object.keys(obj).map((key) => {
            if (Array.isArray(obj[key])) {
                obj[key].forEach(v => {
                    array.push({ key, value: v })
                })
            } else {
                array.push({ key, value: obj[key] })
            }
        });
        url += array.map(x => x.key + "=" + x.value).join("&")
    }
    return url;
}


export class ExpeditionOrdersRestApi {
    prefixUrl = "/api/expeditionOrders";
    deliveryUrl = "/api/deliveryOrders";

    constructor(private restApiServer) {
      
    }

    getOrders(options?): Promise<Array<any>> {
        // return Promise.resolve([]);
        return httpResultCallback(this.restApiServer(parseUrl(this.prefixUrl, options)).get());
    }

    // deliverBatchExpeditionOrder(options, body: Object): Promise<{ orders: Array<ExpeditionOrder>  }> {
    //     return httpResultCallback(this.restApiServer(parseUrl(this.deliveryUrl,options)).post(body));
    // }

    deliverBatchExpeditionOrder( body: Object): Promise<{ orders: Array<ExpeditionOrder>  }> {
        return httpResultCallback(this.restApiServer(parseUrl(this.deliveryUrl)).post(body));
    }

    //应该叫create还是add
    createOrder(order) {
        return httpResultCallback(this.restApiServer(parseUrl(this.prefixUrl)).post({ order }));
    }
}

export const expeditionOrdersRestApi = new ExpeditionOrdersRestApi(businsessHttpServer);


export class EmployeesRestApi {
    prefixUrl = "/api/employees";

    constructor(private restApiServer) {
    
    }
    getEmployees(options?): Promise<Array<any>> {
        return httpResultCallback(this.restApiServer(parseUrl(this.prefixUrl,options)).get());
    }

    updateEmployee(employee:Employee):Promise<Employee>{
       return httpResultCallback(this.restApiServer(parseUrl(this.prefixUrl,{id:employee.id})).put({employee})); 
    }
}

export const employeesRestApi = new EmployeesRestApi(businsessHttpServer);



